package org.jdsm.flow.core.simpleblog;

import org.jdsm.flow.core.simpleblog.dao.IBlogDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: mikom
 * Date: 11/5/12
 * Time: 6:05 PM
 */
public class BlogService implements IBlogService {
    private static final Logger log = LoggerFactory.getLogger(BlogService.class);
    @Resource
    private IBlogDAO blogDAO;

    @Override
    public IPost findPost(IPost post) {
        log.debug("find post:{}", post);
        post = blogDAO.loadPost(post);
        log.debug("post founded:{}", post);
        return post;
    }

    @Override
    public void saveOrUpdate(IPost post) {
        log.debug("save or update post:{}", post);
        blogDAO.saveOrUpdate(post);
        log.info("post: {} was saved or updated ", post);
    }

    @Override
    public void delete(IPost post) {
        log.debug("delete post: {}", post);
        blogDAO.remove(post);
        log.info("post: {} was removed", post);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<IPost> findLatestPosts(int count) {
        log.debug("find latest posts with count:{}", count);
        if (count <= 0) {
            log.debug("set count to 10 records");
            count = 10;
        }
        List<IPost> result = blogDAO.findLatestPosts(count);
        log.debug("founded count posts:{}", result.size());
        return result;
    }
}
